#!/usr/bin/env bash
set -e

BACKUP_DIR="$(cd "$(dirname "$0")" && pwd)"
DATE=$(date +%F_%H-%M-%S)
BACKUP_FILE="$BACKUP_DIR/jenkins_home_$DATE.tgz"

echo "[INFO] 停止 Jenkins 容器..."
docker-compose stop jenkins

echo "[INFO] 开始备份 volume: jenkins_home"
# docker run --rm -v jenkins_home:/data -v "$BACKUP_DIR":/backup alpine \
  sh -c "cd /data && tar czf /backup/jenkins_home_$DATE.tgz ."

cd jenkins_home/ && \
   tar czf /backup/jenkins_home_$DATE.tgz \
   config.xml \
   credentials.xml \
   secret.key secret.key.not-so-secret \
   identity.key.enc \
   secrets/ \
   jobs/ \
   nodes/ \
   users/ \
   plugins/ \
   com.dabsquared.gitlabjenkins.connection.GitLabConnectionConfig.xml \
   com.dabsquared.gitlabjenkins.GitLabPushTrigger.xml \
   hudson.tasks.Maven.xml \
   jenkins.mvn.GlobalMavenConfig.xml \
   org.jenkinsci.plugins.docker.commons.tools.DockerTool.xml \
   org.jenkinsci.plugins.ansible.AnsibleInstallation.xml



echo "[INFO] 备份完成: $BACKUP_FILE"

echo "[INFO] 启动 Jenkins 容器..."
docker-compose start jenkins